package com.zx.stock.controllers;

import com.zx.stock.entity.StockSearch;
import com.zx.stock.service.StockSearchService;
import com.zx.stock.utils.JSONUtil;
import com.zx.stock.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/search")
public class SearchController {

    @Resource
    private StockSearchService stockSearchService;

    //列表
    @ResponseBody
    @RequestMapping(value = "/filter",method = RequestMethod.GET)
    public String list(){

       new Thread(){
                @Override
                public void run(){
                    stockSearchService.analyze();
                }
       }.start();
        return "线程执行，请耐心等待...";
    }

    //列表
    @ResponseBody
    @RequestMapping(value = "/icbc",method = RequestMethod.GET)
    public String findByIcbc(){

        new Thread(){
            @Override
            public void run(){
                stockSearchService.searchIcbcAgency();
            }
        }.start();
        return "线程执行，请耐心等待...";
    }

    @RequestMapping("/code/{code}")
    @ResponseBody
    public String installStatusChange(HttpServletRequest request, @PathVariable String code) {

        if (StringUtil.isEmpty(code)){
            return "参数code不为空";
        }
        StockSearch entity = stockSearchService.findByCode(code);
        if (entity==null){
            return "匹配不符合要求";
        }
        return JSONUtil.objectToJson(entity);
    }
}
